package org.erp.product.strategy.impl;

import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.erp.common.annotation.StockLockBatch;
import org.erp.product.domain.dto.DickerDto;
import org.erp.product.domain.dto.TransferDto;
import org.erp.product.domain.dto.TransferLogAddDto;
import org.erp.product.domain.entity.RepairInfo;
import org.erp.product.infrastructure.enums.EventCodeEnum;
import org.erp.product.infrastructure.enums.RepairEventEnum;
import org.erp.product.service.ConnectLogService;
import org.erp.product.service.RepairInfoService;
import org.erp.product.service.TransferLogService;
import org.erp.product.strategy.TransferSourceStrategy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Log4j2
@Service(value = "REPAIR")
@AllArgsConstructor
public class RepairTransferSourceStrategy implements TransferSourceStrategy {

    private final ConnectLogService connectLogService;

    private final RepairInfoService repairInfoService;

    private final TransferLogService transferLogService;

    @StockLockBatch(listField = "dickerDtoList", idField = "stockId")
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void process(TransferDto transferDto) {
        List<DickerDto> filterStockIds = connectLogService.checkForsake(transferDto.getDickerDtoList());
        List<RepairInfo> repairInfoList = repairInfoService.searchAndVerify(filterStockIds, RepairEventEnum.TRANSFER);
        TransferLogAddDto transferLogAddDto = repairInfoService.updateBatchStatus(repairInfoList, RepairEventEnum.TRANSFER);
        connectLogService.repairSaveBatch(transferDto, repairInfoList, transferLogAddDto);
        transferLogService.repairSaveBatch(transferDto, transferLogAddDto, EventCodeEnum.REPAIR, RepairEventEnum.TRANSFER);
    }
}
